package com.example.covid19.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.Demander;
import com.example.covid19.entity.Stustatus;
import com.example.covid19.mapper.DemanderMapper;
import com.example.covid19.mapper.StustatusMapper;
import com.example.covid19.service.DemanderService;
import com.example.covid19.service.StustatusService;
import com.example.covid19.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiZhengChun
 * @since 2023-03-07
 */
@RestController
@RequestMapping("/demander")
public class DemanderController {
    @Autowired
    StustatusService stustatusService;
    @Autowired
    MessageUtil messageUtil;
    private final DemanderService demanderService;
    private final DemanderMapper demanderMapper;

    private final StustatusMapper stustatusMapper;

    public DemanderController(DemanderService demanderService, DemanderMapper demanderMapper, StustatusMapper stustatusMapper) {
        this.demanderService = demanderService;
        this.demanderMapper = demanderMapper;
        this.stustatusMapper = stustatusMapper;
    }
    @RequestMapping("/afl")
    public Result afl(@RequestBody Demander demander, HttpServletRequest httpServletRequest){
        String token = (String) StpUtil.getLoginIdByToken(httpServletRequest.getHeader("satoken"));
        UUID uuid = UUID.randomUUID();
        String uuidString = uuid.toString();
        Demander demander1 = new Demander();
        Stustatus stustatus = new Stustatus();
        stustatus.setStudentid(Integer.valueOf(token));
        stustatus.setIsleave(1);
        stustatus.setCcid(uuidString);
        stustatusService.updateById(stustatus);
        demander1.setAuid(demander.getAuid());
        demander1.setSttime(demander.getSttime());
        demander1.setEntime(demander.getEntime());
        demander1.setRaison(demander.getRaison());
        demander1.setStudentid(Integer.valueOf(token));
        demander1.setAuid(demander.getAuid());
        demander1.setIstrue(0);
        demander1.setCcid(uuidString);
        System.out.println(demander.getAuid());
        boolean flag = demanderService.save(demander1);
        if (flag){
            messageUtil.sendtwo(String.valueOf(Integer.valueOf(token)),1,demander.getAuid());
            return Result.success(200,"请假成功",null);
        }else {
            return Result.fail(400,"请假失败",null);
        }
    }

    /**
     *
     * @param httpServletRequest
     * @return检查是否存在请假，如果还没有经过审核那么前端不允许展示表单页面
     */
    @RequestMapping("ifafl")
    public Result ifafl(HttpServletRequest httpServletRequest){
        String token = (String) StpUtil.getLoginIdByToken(httpServletRequest.getHeader("satoken"));
        if (stustatusService.getOne(new QueryWrapper<Stustatus>().eq("studentid",token)).getIsleave()==0){
            return Result.success(200,"当前不存在请假",null);
        }else {
            Demander demander = demanderService.getOne(new QueryWrapper<Demander>().eq("ccid",stustatusService.getOne(new QueryWrapper<Stustatus>().eq("studentid",token)).getCcid()));
            if (demander.getIstrue()==0){
                return Result.success(201,"等待审核",null);
            }
        }

        return null;
    }
    @RequestMapping("/checkdem")
    public Result checkdem(HttpServletRequest httpServletRequest){
        String token = (String) StpUtil.getLoginIdByToken(httpServletRequest.getHeader("satoken"));
        if (stustatusService.getOne(new QueryWrapper<Stustatus>().eq("studentid",token)).getIsleave()==0){
            return Result.success(200,"可以请假",null);
        }else {
            return Result.fail(400,"已存在记录，不可请假",null);
        }
    }

    @RequestMapping("/getmyafl")
    public Result getmyafl(HttpServletRequest httpServletRequest){
        String token = (String) StpUtil.getLoginIdByToken(httpServletRequest.getHeader("satoken"));
        return Result.success(200,"返回信息",demanderService.list(new QueryWrapper<Demander>().eq("studentid",token)));
    }
    @RequestMapping("/stinfo")
    public Result stinfo(HttpServletRequest httpServletRequest){
        String token = (String) StpUtil.getLoginIdByToken(httpServletRequest.getHeader("satoken"));
        return Result.success(200,"成功",demanderMapper.stinfo(token));
    }
}
